package com.example.zhidao.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.zhidao.dao.BlogMapper;
import com.example.zhidao.dao.GoodsMapper;
import com.example.zhidao.model.po.Goods;
import com.example.zhidao.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;

@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements GoodsService {

    @Resource
    GoodsMapper goodsMapper;

    @Resource
    BlogMapper blogMapper;

//    事务点赞的同时，文章的点赞数加一
@Transactional(rollbackFor = Exception.class)
    @Override
    public boolean addgood(Goods goods) {
    try{
        goodsMapper.insert(goods);
        blogMapper.addgood(goods.getBid());
    }catch (Exception e){
        // 回滚事务
        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        return false;
    }

        return true;
    }


    //    事务点赞的同时，文章的点赞数减一
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean delgood(Goods goods) {
        try{
            goodsMapper.deleteById(goods);
            blogMapper.delgood(goods.getBid());
        }catch (Exception e){
            // 回滚事务
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }

        return true;
    }
}
